package com.cjdes.recipes4u.jaxb;

import java.net.URL;

import org.xml.sax.InputSource;


import ae.javax.xml.bind.JAXBContext;
import ae.javax.xml.bind.Unmarshaller;
import android.os.AsyncTask;

import com.cjdes.android.AsyncTaskResult;
import com.cjdes.recipes4u.MainActivity;
import com.cjdes.recipes4u.dto.RecipeStore;

public class RecipeUnpacker extends AsyncTask<URL, String, AsyncTaskResult<RecipeStore>>{
	
	
	public static AsyncTaskResult<RecipeStore> getRecipeStore()
			throws Exception {
		
		RecipeUnpacker unpacker = new RecipeUnpacker();
		URL url = new URL("http://www.cj-des.com/recipes.xml");
		return unpacker.execute(url).get();	
	

	}

	@Override
	protected AsyncTaskResult<RecipeStore> doInBackground(URL... params) {
		try{
			JAXBContext jaxbContext = JAXBContext.newInstance(RecipeStore.class);
			Unmarshaller um = jaxbContext.createUnmarshaller();
			URL url = params[0];
			InputSource source = new InputSource(url.openStream());
			
			return new AsyncTaskResult<RecipeStore>((RecipeStore)um.unmarshal(source));
		} catch (Exception ex){
			return new AsyncTaskResult<RecipeStore>(ex);
			
		}
	}

	
	
	

}
